/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.MaintenanceStaffDevice;
import com.zhuoyue.elmt.service.MaintenanceStaffDeviceService;

import java.util.List;

/**
 * 维保人员备案设备信息Controller
 * @author chenhao
 * @version 2016-09-11
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/maintenanceStaffDevice")
public class MaintenanceStaffDeviceController extends BaseController {

	@Autowired
	private MaintenanceStaffDeviceService maintenanceStaffDeviceService;
	
	@ModelAttribute
	public MaintenanceStaffDevice get(@RequestParam(required=false) String id) {
		MaintenanceStaffDevice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = maintenanceStaffDeviceService.get(id);
		}
		if (entity == null){
			entity = new MaintenanceStaffDevice();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:maintenanceStaffDevice:view")
	@RequestMapping(value = {"list", ""})
	public String list(MaintenanceStaffDevice maintenanceStaffDevice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MaintenanceStaffDevice> page = maintenanceStaffDeviceService.findPage(new Page<MaintenanceStaffDevice>(request, response), maintenanceStaffDevice); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/maintenanceStaffDeviceList";
	}

	@RequiresPermissions("elmt:maintenanceStaffDevice:view")
	@RequestMapping(value = "form")
	public String form(MaintenanceStaffDevice maintenanceStaffDevice, Model model) {
		model.addAttribute("maintenanceStaffDevice", maintenanceStaffDevice);
		return "zhuoyue/elmt/maintenanceStaffDeviceForm";
	}

	@RequiresPermissions("elmt:maintenanceStaffDevice:edit")
	@RequestMapping(value = "save")
	public String save(MaintenanceStaffDevice maintenanceStaffDevice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, maintenanceStaffDevice)){
			return form(maintenanceStaffDevice, model);
		}
		maintenanceStaffDeviceService.save(maintenanceStaffDevice);
		addMessage(redirectAttributes, "保存维保人员备案设备信息成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/maintenanceStaffDevice/?repage";
	}
	
	@RequiresPermissions("elmt:maintenanceStaffDevice:edit")
	@RequestMapping(value = "delete")
	public String delete(MaintenanceStaffDevice maintenanceStaffDevice, RedirectAttributes redirectAttributes) {
		maintenanceStaffDeviceService.delete(maintenanceStaffDevice);
		addMessage(redirectAttributes, "删除维保人员备案设备信息成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/maintenanceStaffDevice/?repage";
	}

	@RequestMapping(value = "getByDeviceCode")
	@ResponseBody
	public String getByDeviceCode(String sbzcdm,Model model, RedirectAttributes redirectAttributes) {
		String flag = "";
		String[] codes = sbzcdm.split(",");
		List<MaintenanceStaffDevice> stafflist = maintenanceStaffDeviceService.getByDeviceCode(codes);
		if(stafflist.size() > 0){
			flag = "1";
		} else {
			flag = "2";
		}
		return flag;
	}
}